package cn.sportvip.controller.sport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.geo.Point;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.sportvip.Sport.AGE;
import cn.sportvip.Sport.HIS;
import cn.sportvip.dto.sport.CoachDto;
import cn.sportvip.dto.sport.CourseDto;
import cn.sportvip.dto.sport.GymnasiumDto;
import cn.sportvip.entity.sport.CoachBasic;
import cn.sportvip.entity.sport.CourseHistory;
import cn.sportvip.entity.sport.Event;
import cn.sportvip.entity.sport.GymnasiumBasic;
import cn.sportvip.entity.sport.ServicePhone;
import cn.sportvip.service.sport.SportService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping("sport")
public class SportController {

	@Autowired
	private SportService sportService;

	/** 上传图片 */
	@RequestMapping("uploadFile")
	public JSONObject uploadFile(@RequestParam MultipartFile file) {
		JSONObject result = new JSONObject();
		result.put("fileName", sportService.uploadFile(file));
		return result;
	}

	/** 查看附近场馆 */
	@RequestMapping("nearbyGymnasiums")
	public Page<GymnasiumBasic> nearbyGymnasiums(@RequestParam double lat,
			@RequestParam double lng,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "15") int pageSize,
			@RequestParam(required = false, defaultValue = "0") int distance,
			AGE age, @RequestParam(required = false, defaultValue = "0") int eventId) {
		Point location = new Point(lat, lng);
		Pageable pageable = new PageRequest(pageNo - 1, pageSize);
		return sportService.nearbyGymnasiums(location, pageable, distance, age, eventId);
	}
	/**查看场馆详情*/
	@RequestMapping("gymnasium")
	public GymnasiumDto GymnasiumDetial(@RequestParam int gymnasiumId) {
		return sportService.findGymnasiumById(gymnasiumId);
	}
	/** 查看附近教练 */
	@RequestMapping("nearbyCoaches")
	public Page<CoachBasic> nearByCoaches(@RequestParam double lat,
			@RequestParam double lng,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "15") int pageSize,
			@RequestParam(required = false, defaultValue = "0") int distance,
			AGE age, @RequestParam(required = false, defaultValue = "0") int eventId) {
		Point location = new Point(lat, lng);
		Pageable pageable = new PageRequest(pageNo - 1, pageSize);
		return sportService.nearbyCoaches(location, pageable, distance, age, eventId);
	}
	/**查看教练详情*/
	@RequestMapping("coach")
	public CoachDto coachDetial(@RequestParam int coachId) {
		return sportService.findCoachById(coachId);
	}
	/**查看系统中所有的运动项目名称*/
	@RequestMapping("events")
	public Iterable<Event> findAllEvents() {
		return sportService.findAllEvents();
	}
	/**城市列表*/
	@RequestMapping("cities")
	public JSONArray findAllCities() {
		JSONObject city = new JSONObject();
		city.put("cityName", "上海");
		city.put("cityCode", "021");
		
		JSONArray result = new JSONArray();
		result.add(city);
		return result;
	}
	/**课程详情*/
	@RequestMapping("course")
	public CourseDto courseDetial(@RequestParam int courseId) {
		return sportService.findCourseDto(courseId);
	}
	/**添加课程历史*/
	@RequestMapping("coursehistory")
	public CourseHistory addCourseHistory(CourseHistory history) {
		return sportService.addCourseHistory(history);
	}
	/**查找我查看/联系过的课程*/
	@RequestMapping("findcoursehistory")
	public Object findCourseHistory(@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "15") int pageSize,
			@RequestParam int userId, @RequestParam HIS type,
			@RequestParam(required = false, defaultValue = "0") double lat, @RequestParam(required = false, defaultValue = "0") double lng) {
		Pageable pageable = new PageRequest(pageNo - 1, pageSize);
		return sportService.findCourseHistory(userId, type, new Point(lat, lng), pageable);
	}
	/**客服电话*/
	@RequestMapping("servicephone")
	public Iterable<ServicePhone> findAllServicePhone() {
		return sportService.findAllServicePhone();
	}
}
